#include "top.h"

void print_member_info(const member_info* mi);

//暂停15秒
void my_pause(void)
{
        system("clear");
        printf("\n\n\n输入错误次数太多，请您在 15 秒后重试！\n\n\n\n");
        sleep(15);
	getchar();
        press_any_key();
        system("clear");
}



//任意键返回
void press_any_key(void)
{
         printf("\n请按任意键进入下一内容...\n");
        //关闭行缓冲功能和回显
        system("stty -icanon -echo");
        getchar();
        //恢复终端行缓冲和回显
        system("stty icanon echo");
}



//获取mid
static unsigned int get_new_mid(void)
{
        member_info mi;
        unsigned int mid;

        FILE* fp = fopen(MEMBER_INFO_FILE,"rb");

        if(fp == NULL)
        {

                return  MEMBER_ID_BASE;
        }

        fseek(fp,-sizeof(mi),SEEK_END);
        if(fread(&mi, sizeof(mi), 1, fp) == 1)
        {
                mid = mi.mid+1;
        }
        else
        {
                mid = MEMBER_ID_BASE;
        }


        fclose(fp);
        return mid;
}




//增加会员用户
void member_reg(void)
{
	system("clear");
	member_info mi;

	printf("请按照如下提示输入会员信息\n\n\n");
	printf("卡号：");
	scanf("%s",mi.card_num);
	printf("姓名：");
	scanf("%s",mi.name);
	printf("性别(1-女,2-男)：");
	scanf("%d",(int*)&mi.s);
	printf("手机号：");
	scanf("%s",mi.phone);
	printf("会员类型（1-次卡，2，3）");
	scanf("%d",(int*)&mi.mt);
	if(mi.mt == cika)

	{
		printf("剩余次数：");
		scanf("%d",&mi.remain_cnt);
	}


	mi.reg_time = time(NULL);//注册时间

	if(mi.mt == yueka)
		mi.expire_time = mi.reg_time + 30 * 24 * 60 * 60;
	else
		mi.expire_time = mi.reg_time + 365 * 24 * 60 * 60;

	mi.mid = get_new_mid();
	mi.delete_state = 0;

	FILE* fp = fopen(MEMBER_INFO_FILE, "ab");

	if(fp == NULL)
	{
		perror("会员注册失败");
		return;
	}
	fwrite(&mi,sizeof(mi),1,fp);

	fclose(fp);
	printf("\n会员注册成功!\n");

}




//查看个人信息
void member_show(void)
{
	system("clear");
	member_info mi;
	char card_num[11];

	system("clear");

	printf("\n会员请刷卡！\n");
	scanf("%s",card_num);
	getchar();
	FILE* fp = fopen(MEMBER_INFO_FILE,"rb");
	if(fp == NULL)
	{
		printf("\n\n\n卡片未注册，请联系工作人员处理！\n\n\n");
		press_any_key();
		return;
	}

	while(fread(&mi,sizeof(mi),1,fp) == 1)
	{
		if(strcmp(mi.card_num, card_num) == 0||strcmp(card_num,mi.phone) == 0||atol(card_num) == mi.mid)
		{
			break;
		}
	}

	if(feof(fp))
	{
		printf("\n\n\n卡片未注册，请联系工作人员处理！\n\n\n");
	}
	else
	{
		if(mi.delete_state == 1)
		{
			printf("\n\n\n此卡已注销，请联系工作人员处理！\n\n\n");
		}

		else
		{
			printf("\n尊贵的会员，您的信息如下：\n");
			printf("卡号：%s\n",mi.card_num);
			printf("会员ID：%u\n",mi.mid);
			printf("姓名：%s\n",mi.name);
			printf("性别：%s\n",mi.s == female?"女":"男");
			printf("手机号：%s\n",mi.phone);

			printf("会员类型：");
			if(mi.mt == cika)
			{
				printf("次卡\n剩余次数：%d\n",mi.remain_cnt);
			}
			else if(mi.mt == yueka)
			{
				printf("月卡\n");
			}
			else if(mi.mt == nianka)
			{
				printf("年卡\n");
			}

			struct tm* dt;

			dt = localtime(&mi.expire_time);

			printf("到期时间：%d年%d月%d日 %02d：%02d\n",dt->tm_year + 1900,dt->tm_mon + 1,dt->tm_mday,dt->tm_hour,dt->tm_min);

			dt = localtime(&mi.reg_time);

			printf("注册时间：%d年%d月%d日 %02d：%02d\n",dt->tm_year + 1900,dt->tm_mon + 1,dt->tm_mday,dt->tm_hour,dt->tm_min);

			if(mi.mt == yueka || mi.mt == nianka)
			{
				printf("剩余时间：%ld 天\n", (mi.expire_time - time(NULL))/(60*60*24));
			}

		}
	}

	fclose(fp);

	press_any_key();

}


//注销会员
void member_del(void)
{
	system("clear");
	char num[50];
	member_info mi;
	printf("请输入要注销会员卡号/会员ID/手机号：");
	scanf("%s", num);

	getchar();
	FILE* fp = fopen(MEMBER_INFO_FILE,"rb+");

	while(fread(&mi,sizeof(mi),1,fp) == 1)
	{
		if(strcmp(num,mi.card_num) == 0 ||strcmp(num,mi.phone) == 0||atol(num) == mi.mid)
		{

			break;
		}
	}
	if(feof(fp))
	{
		printf("\n不存在此会员，注销失败！\n");
	}
	else
	{
		char select;
		printf("\n要注销的会员信息：如下\n");
		print_member_info(&mi);

		printf("\n是否注销（Y是/N否）:");
		select = getchar();
		if(select == 'Y'||select == 'y')
		{
			mi.delete_state = 1;

			fseek(fp,-sizeof(mi),SEEK_CUR);

			fwrite(&mi,sizeof(mi),1,fp);
			fflush(fp);

			printf("\n\n\n会员已注销！！！\n\n\n");
		}

	}

	fclose(fp);

	getchar();
	press_any_key();
}



//会员信息
void print_member_info(const member_info* mi)
{
		printf("----------------------------------\n");
                printf("卡号：%s\n",mi->card_num);
                printf("会员ID：%u\n",mi->mid);
                printf("姓名：%s\n",mi->name);
                printf("性别：%s\n",mi->s == female?"女":"男");
                printf("手机号：%s\n",mi->phone);
		
                printf("会员类型：");
                if(mi->mt == cika)
                {
                        printf("次卡\n剩余次数：%d\n",mi->remain_cnt);
                }
                else if(mi->mt == yueka)
                {
                        printf("月卡\n");
                }
                else if(mi->mt == nianka)
                {
                        printf("年卡\n");
                }

                struct tm* dt;

                dt = localtime(&mi->expire_time);

                printf("到期时间：%d年%d月%d日 %02d：%02d\n",dt->tm_year + 1900,dt->tm_mon + 1,dt->tm_mday,dt->tm_hour,dt->tm_min);

                dt = localtime(&mi->reg_time);

                printf("注册时间：%d年%d月%d日 %02d：%02d\n",dt->tm_year + 1900,dt->tm_mon + 1,dt->tm_mday,dt->tm_hour,dt->tm_min);

		if(mi->mt == yueka || mi->mt == nianka)
                        {
                                printf("剩余时间：%ld 天\n", (mi->expire_time - time(NULL))/(60*60*24));
                        }


		if(mi->delete_state == 1)
		{
			printf("状态：**已注销**\n");
		}
}



//恢复注销会员信息
void remember(void)
{
	system("clear");
	char num[50];
	member_info mi;
	printf("请输入要恢复会员卡号/会员ID/手机号：");
	scanf("%s", num);

	getchar();
	FILE* fp = fopen(MEMBER_INFO_FILE,"rb+");

	while(fread(&mi,sizeof(mi),1,fp) == 1)
	{
		if(strcmp(num,mi.card_num) == 0 ||strcmp(num,mi.phone) == 0||atol(num) == mi.mid)
		{

			break;
		}
	}
	if(feof(fp))
	{
		printf("\n不存在此会员，恢复信息失败！\n");
	}
	else
	{

		char select;
		printf("\n要恢复的会员信息：如下\n");
		print_member_info(&mi);

		printf("\n是否恢复（Y是/N否）:");
		select = getchar();
		if(select == 'Y'||select == 'y')
		{
			mi.delete_state = 0;

			fseek(fp,-sizeof(mi),SEEK_CUR);

			fwrite(&mi,sizeof(mi),1,fp);
			fflush(fp);

			printf("\n会员已恢复！\n");
		}

	}

	fclose(fp);
	getchar();
	press_any_key();

}



//打印所有人(未注销)信息
void all_member(void)
{
	member_info mi;
	FILE* fp = fopen(MEMBER_INFO_FILE,"rb+");

        while(fread(&mi,sizeof(mi),1,fp) == 1)
        {
		if(mi.delete_state == 0)
		{
			print_member_info(&mi);
			printf("\n");
		}

        }


}



//查看是否重复注册
int re_member()
{
	char h[30];
	printf("请输入卡号/手机号/ID：");
	scanf("%s",h);
	getchar();
        member_info mi;
        FILE* fp = fopen(MEMBER_INFO_FILE,"rb+");
        while(fread(&mi,sizeof(mi),1,fp) == 1)
        {
                if(strcmp(h,mi.card_num) == 0 ||strcmp(h,mi.phone) == 0||atol(h) == mi.mid)
                {

                        break;
                }
        }
        if(feof(fp))
        {
                printf("\n不存在此会员，请按照下面提示注册\n");
        }
        else
                {
                        return 1;

                }

        fclose(fp);

	return 0;
}




//次卡消次数
void less_cika(void)
{
	system("clear");
	char num[50];
	member_info mi;
	printf("请输入会员卡号/会员ID/手机号：");
	scanf("%s",num);
	getchar();
	FILE* fp = fopen(MEMBER_INFO_FILE,"rb+");
	while(fread(&mi,sizeof(mi),1,fp) == 1)
	{
		if(strcmp(num,mi.card_num) == 0 ||strcmp(num,mi.phone) == 0||atol(num) == mi.mid)
		{

			break;
		}
	}
	if(feof(fp))
	{
		printf("未查询到会员信息，请联系管理员注册！\n");
	}
	else
	{
		fseek(fp, -24, SEEK_CUR);
		if(mi.remain_cnt == 0)
		{
			printf("\n\n\n您的卡号剩余次数，已不足扣除本次！\n请您联系管理员进行充值！\n\n\n\n");
		}
		else
		{
			mi.remain_cnt--;
			fwrite(&(mi.remain_cnt),sizeof(mi.remain_cnt),1,fp);
			print_member_info(&mi);
		}
		press_any_key();
	}
	fclose(fp);
}



//增加次数/改变天数
void change_cnt_day()
{
	system("clear");
        char num[50];
        member_info mi;
        printf("请输入会员卡号/会员ID/手机号：");
        scanf("%s",num);
        getchar();
        FILE* fp = fopen(MEMBER_INFO_FILE,"rb+");
        while(fread(&mi,sizeof(mi),1,fp) == 1)
        {
                if(strcmp(num,mi.card_num) == 0 ||strcmp(num,mi.phone) == 0||atol(num) == mi.mid)
                {

                        break;
                }
        }
        if(feof(fp))
        {
                printf("未查询到会员信息，请联系管理员注册！\n");
        }
        else if(mi.mt == cika)
        {
                fseek(fp, -24, SEEK_CUR);
		int cnt;
		printf("请输入您要增加的次数：");
		scanf("%d",&cnt);
		printf("\n");
		mi.remain_cnt += cnt;
                fwrite(&(mi.remain_cnt),sizeof(mi.remain_cnt),1,fp);
                print_member_info(&mi);
        }
	else
	{
		int day;
		printf("请输入您要增加的天数：");
		scanf("%d",&day);
		printf("\n");
		day = day * 60*60*24;
		fseek(fp, -16,SEEK_CUR);
		mi.expire_time += day;
		fwrite(&(mi.expire_time), sizeof(mi.expire_time),1,fp);
                print_member_info(&mi);
	}
        fclose(fp);
	getchar();
        press_any_key();


}

//密码
void passwd(void)
{
	while(1)
	{
		system("clear");
		char select;
		printf("\n是否输入帐号密码（Y是/N否）：");
		getchar();
		select = getchar();
		if(select == 'Y'||select == 'y')
		{
			system("clear");
			char nums[20];
			printf("账号:");
			scanf("%s",nums);
			getchar();
			if(strcmp("chen",nums) == 0)
			{
				char nums2[20];
				printf("密码:");
				scanf("%s",nums2);
				getchar();
				if(strcmp("123456",nums2) == 0)
				{
					root();
					break;
				}
				else
				{
					printf("\n密码有误，请重新输入！\n");
					break;
				}
			}
			else
			{
				printf("\n账号有误，请重新输入！\n");
				break;
			}
		}
		else
		{
			break;
		}
	}
}


//管理员用户
void root()
{

	while(1)
	{
		system("clear");

		printf("\n\n**********管理员界面**********\n\n\n\n");
                printf("** 1.增加会员信息 **\n");
                printf("** 2.查看所有会员信息 **\n");
                printf("** 3.恢复注销会员信息 **\n");
                printf("** 4.注销会员信息 **\n");
                printf("** 5.增加天/次数 **\n");
                printf("** 0.退出管理员系统 **\n\n\n");

		int op;
		printf("请输入功能序号:>");
		if(scanf("%d",&op) != 1)
		{
			while(getchar() != '\n');
			op = -1;
		}

		getchar();	
		switch(op)
		{
			case 1:
				{
					//增加会员
					if(re_member() == 0)
					{
						member_reg();
						getchar();
						press_any_key();
					}
					else
					{
						printf("\n会员信息已存在,返回后查看\n");
                                                press_any_key();
					}
				}
				break;
			case 2:
				{
					//查看/修改会员信息
					system("clear");
					all_member();
					press_any_key();
				}
				break;
			case 3:
				{
					//恢复注销会员信息
					remember();
				}
				break;
			case 4:
				{
					//注销会员信息
					member_del();
				}
				break;
			case 5:
				{
					//增加次数
					change_cnt_day();
				}
				break;
			case 0:
				{
					printf("\n已退出管理员模式！\n");

					return;

				}
				break;
			default:
				{
					//输入错误
					printf("输入有误，请重新输入");
				}
				break;



		}
	}

}







